//
//  PickerView.m
//  DaJiang
//
//  Created by sally on 16/10/31.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "PickerView.h"

@interface PickerView ()

@property (nonatomic, readonly) PickerType showType;

@end

@implementation PickerView

- (void)initControl {
  self.pickerView.delegate = [[CommonMethod sharedCommonMethod] getCurrViewController];
  self.pickerView.dataSource = [[CommonMethod sharedCommonMethod] getCurrViewController];
}

- (instancetype)initWithPickerType:(PickerType)type {
  NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"PickerView" owner:nil options:nil];
  self = nibArray[0];

  if (self) {
    _showType = type;
    [self initControl];
  }
  return self;
}

- (void)show {
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  [window addSubview:self];
  self.translatesAutoresizingMaskIntoConstraints = NO;
  [[CommonMethod sharedCommonMethod] setEdge:window view:self edgeInset:UIEdgeInsetsMake(0, 0, 0, 0)];
  [self.pickerView reloadAllComponents];
}

- (void)showPickerInView:(UIView *)viewT {
  BOOL needAdd = true;
  for (UIView *view in viewT.subviews) {
    if ([view isKindOfClass:[self class]]) {
      needAdd = false;
      break;
    }
  }
  if (needAdd == true) {
    [viewT addSubview:self];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [[CommonMethod sharedCommonMethod] setEdge:viewT view:self edgeInset:UIEdgeInsetsMake(0, 0, 0, 0)];
  }
}

- (void)hidePickerView {
  [self removeFromSuperview];
}

- (IBAction)actionOnSure:(id)sender {
  [self hidePickerView];
}

- (IBAction)actionOnCancel:(id)sender {
  [self hidePickerView];
  if (self.delegate && [self.delegate respondsToSelector:@selector(cancelSelect:)]) {
    [self.delegate cancelSelect:self];
  }
}

@end
